﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControllerLayer.PatientInformation;
using ModelLayer.Login;

namespace ViewLayer.PatientInformationView
{
    public partial class PatientNoteControl : UserControl
    {
        private string patientOID;

        public PatientNoteControl(string patientOID)
        {
            InitializeComponent();
            this.patientOID = patientOID;

            this.patientNoteCategory_TableAdapter.Fill(this.patientNoteDataSet.PatientNoteCategory_);

            try
            {
                this.patientNote_TableAdapter.Fill(this.patientNoteDataSet.PatientNote_, new System.Guid(this.patientOID));
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void buttonNewNote_Click(object sender, EventArgs e)
        {
            categoryOID_ComboBox.Enabled = true;
            comment_TextBox.Enabled = true;
            buttonNewCategory.Enabled = true;
            buttonSaveNote.Enabled = true;
            buttonCancelNote.Enabled = true;

            comment_TextBox.Clear();
        }

        private void buttonSaveNote_Click(object sender, EventArgs e)
        {
            categoryOID_ComboBox.Enabled = false;
            comment_TextBox.Enabled = false;
            buttonNewCategory.Enabled = false;
            buttonSaveNote.Enabled = false;
            buttonCancelNote.Enabled = false;

            User user = User.Instance;
            if (!string.IsNullOrEmpty(comment_TextBox.Text) && !string.IsNullOrEmpty(categoryOID_ComboBox.SelectedValue.ToString()))
                this.patientNote_TableAdapter.InsertQuery(new Guid(this.patientOID), DateTime.Now, user.getStaffOID(),new Guid(categoryOID_ComboBox.SelectedValue.ToString()), comment_TextBox.Text);

            this.patientNote_TableAdapter.Fill(this.patientNoteDataSet.PatientNote_, new System.Guid(this.patientOID));
        }

        private void buttonNewCategory_Click(object sender, EventArgs e)
        {
            NewNoteCategoryView categoryView = new NewNoteCategoryView();
            categoryView.ShowDialog();
            this.patientNoteCategory_TableAdapter.Fill(this.patientNoteDataSet.PatientNoteCategory_);
            if (!PatientInformationViewController.newPatientNoteCategoryOID.Equals("none"))
                categoryOID_ComboBox.SelectedValue = PatientInformationViewController.newPatientNoteCategoryOID;
        }

        private void buttonCancelNote_Click(object sender, EventArgs e)
        {
            categoryOID_ComboBox.Enabled = false;
            comment_TextBox.Enabled = false;
            comment_TextBox.Clear();
            buttonNewCategory.Enabled = false;
            buttonSaveNote.Enabled = false;
            buttonCancelNote.Enabled = false;
            this.patientNote_TableAdapter.Fill(this.patientNoteDataSet.PatientNote_, new System.Guid(this.patientOID));
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (patientNote_DataGridView.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("¿Esta seguro que desea eliminar la nota del paciente?", "Eliminar nota de paciente", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    this.patientNote_TableAdapter.DeleteQuery(new Guid(this.patientNote_DataGridView[0, patientNote_DataGridView.SelectedRows[0].Index].Value.ToString()));
                    this.patientNote_TableAdapter.Fill(this.patientNoteDataSet.PatientNote_, new System.Guid(this.patientOID));
                }
            }
        }
       
    }
}
